﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Forums
{
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Diagnostics.CodeAnalysis;
    using Microsoft.Phone.Data.Linq.Mapping;

    /// <content>Contains the LINQ to SQL portion of the <see cref="Host"/> class.</content>
    [Table]
    [Index(Columns = "Name,Port", IsUnique = true)]
    [SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "The order of the properties defines the order of database columns.")]
    internal sealed partial class Host
    {
        private string userName;
        private string emailAddress;

        private EntityRef<Root> rootRef;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        private int Id { get; set; }

        [Column(UpdateCheck = UpdateCheck.Never)]
        private int RootId { get; set; }

        [Column(CanBeNull = false, DbType = "NVARCHAR(1024) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        internal string Provider { get; set; }

        /// <summary>See <see cref="IHost.Name"/>.</summary>
        [Column(CanBeNull = false, DbType = "NVARCHAR(254) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string Name { get; internal set; }

        /// <summary>See <see cref="IHost.Port"/>.</summary>
        [Column(UpdateCheck = UpdateCheck.Never)]
        public int Port { get; internal set; }

        /// <summary>See <see cref="IHost.UserName"/>.</summary>
        [Column(Storage = "userName", DbType = "NVARCHAR(64)", UpdateCheck = UpdateCheck.Never)]
        public string UserName
        {
            get
            {
                return this.userName;
            }

            set
            {
                this.AssertValid();
                this.userName = value;
            }
        }

        /// <summary>See <see cref="IHost.EmailAddress"/>.</summary>
        [Column(Storage = "emailAddress", DbType = "NVARCHAR(320)", UpdateCheck = UpdateCheck.Never)]
        public string EmailAddress
        {
            get
            {
                return this.emailAddress;
            }

            set
            {
                this.AssertValid();
                this.emailAddress = value;
            }
        }

        [Column(DbType = "VARBINARY(512)", UpdateCheck = UpdateCheck.Never)]
        internal byte[] AccountNameEncrypted { get; set; }

        [Column(DbType = "VARBINARY(512)", UpdateCheck = UpdateCheck.Never)]
        internal byte[] PasswordEncrypted { get; set; }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [Association(Name = "Root_Host", Storage = "rootRef", ThisKey = "RootId", IsForeignKey = true)]
        private Root RootCore
        {
            get { return this.rootRef.Entity; }
            set { this.rootRef.Entity = value; }
        }

        private void OnValidate(ChangeAction action)
        {
            if (action == ChangeAction.Delete)
            {
                this.Root.AssertSendReceiveNotInProgress();
            }
        }
    }
}
